﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tunnel.Tools;

namespace Tunnel.Devices
{
    /// <summary>
    /// 设备控件基类，其他设备类型空间将会继承此类
    /// </summary>
    public abstract partial class DeviceBase : PictureBox

    {
        string deviceId;
        string deviceStatusId;
        string statusDescription;
        string shortName;
        string typeId;
        string devicePos;
        int picX;
        int picY;
        string protocol;
        string port;
        string controlLable;
        DateTime updateTime;
        string userId;
        string road;
        string factoryId;
        string model;
        string tunnel;
        private Label lbTip = new Label();
        /// <summary>
        /// 设备ID
        /// </summary>
        public virtual string DeviceId
        {
            get
            {
                return deviceId;
            }

            set
            {
                deviceId = value;
            }
        }
        /// <summary>
        /// 设备状态ID
        /// </summary>
        public virtual string DeviceStatusId
        {
            get
            {
                return deviceStatusId;
            }

            set
            {
                deviceStatusId = value;
            }
        }
        /// <summary>
        /// 设备状态描述
        /// </summary>
        public virtual string StatusDescription
        {
            get
            {
                return statusDescription;
            }

            set
            {
                statusDescription = value;
            }
        }
        /// <summary>
        /// 设备简称
        /// </summary>
        public string ShortName
        {
            get
            {
                return shortName;
            }

            set
            {
                shortName = value;
            }
        }
        /// <summary>
        /// 设备类型ID
        /// </summary>
        public virtual string TypeId
        {
            get
            {
                return typeId;
            }

            set
            {
                typeId = value;
            }
        }
        /// <summary>
        /// 设备桩号
        /// </summary>
        public string DevicePos
        {
            get
            {
                return devicePos;
            }

            set
            {
                devicePos = value;
            }
        }
        /// <summary>
        /// 设备图标坐标X
        /// </summary>
        public int PicX
        {
            get
            {
                return picX;
            }

            set
            {
                picX = value;
                this.Location = new Point(PicX, this.Location.Y);
            }
        }
        /// <summary>
        /// 设备图标坐标Y
        /// </summary>
        public int PicY
        {
            get
            {
                return picY;
            }

            set
            {
                picY = value;
                this.Location = new Point(this.Location.X, PicY);
            }
        }
        /// <summary>
        /// 设备协议类型
        /// </summary>
        public string Protocol
        {
            get
            {
                return protocol;
            }

            set
            {
                protocol = value;
            }
        }
        /// <summary>
        /// 设备端口
        /// </summary>
        public string Port
        {
            get
            {
                return port;
            }

            set
            {
                port = value;
            }
        }
        /// <summary>
        /// 设备是否可控
        /// </summary>
        public string ControlLable
        {
            get
            {
                return controlLable;
            }

            set
            {
                controlLable = value;
            }
        }
        /// <summary>
        /// 设备状态更新时间
        /// </summary>
        public DateTime UpdateTime
        {
            get
            {
                return updateTime;
            }

            set
            {
                updateTime = value;
            }
        }
        /// <summary>
        /// 设备状态更新用户ID
        /// </summary>
        public string UserId
        {
            get
            {
                return userId;
            }

            set
            {
                userId = value;
            }
        }
        /// <summary>
        /// 路名
        /// </summary>
        public string Road
        {
            get
            {
                return road;
            }

            set
            {
                road = value;
            }
        }
        /// <summary>
        /// 厂商ID
        /// </summary>
        public string FactoryId
        {
            get
            {
                return factoryId;
            }

            set
            {
                factoryId = value;
            }
        }
        /// <summary>
        /// 设备型号
        /// </summary>
        public string Model
        {
            get
            {
                return model;
            }

            set
            {
                model = value;
            }
        }
        /// <summary>
        /// 隧道名称
        /// </summary>
        public string Tunnel
        {
            get
            {
                return tunnel;
            }

            set
            {
                tunnel = value;
            }
        }
        /// <summary>
        /// 设备基类构造方法
        /// </summary>
        public DeviceBase()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 设备基类绘制方法
        /// </summary>
        /// <param name="pe"></param>
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
            this.DoubleBuffered = true;
            this.lbTip.Parent = this.Parent;
            this.lbTip.Visible = false;

            this.lbTip.Text = $"{this.Name}\r\n桩号：{this.DevicePos}\r\n当前状态：{this.StatusDescription}\r\n{UpdateTime.ToString()}";
            this.lbTip.Font = new Font("等线", 15);
            this.lbTip.BringToFront();
            this.lbTip.AutoSize = true;
            this.lbTip.BackColor = Color.LightYellow;
        }
        /// <summary>
        /// 鼠标移入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DeviceBase_MouseEnter(object sender, EventArgs e)
        {
            //tpName.Show(this.ToString(), this);
            //this.lbTip.Visible = true;
            //this.lbTip.BringToFront();
            //DataDeal.ShowMessage("in");
        }
        /// <summary>
        /// 鼠标移出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DeviceBase_MouseLeave(object sender, EventArgs e)
        {
            //tpName.Hide(this);
            this.lbTip.Visible = false;
            //DataDeal.ShowMessage("out");
        }
        /// <summary>
        /// 设备转换为字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return Name + "\r\n当前状态：" + StatusDescription + "\r\n" + UpdateTime + "\r\n位置:" + DevicePos;
        }
        /// <summary>
        /// 发送指令抽象方法
        /// </summary>
        /// <param name="commandType"></param>
        public abstract void SendCommand(CommandType commandType);

        private void DeviceBase_MouseMove(object sender, MouseEventArgs e)
        {
            var x = e.Location.X + this.Parent.Location.X + this.Location.X + lbTip.Width + 15;
            var y = e.Location.Y + this.Parent.Location.Y + this.Location.Y + lbTip.Height + 15;
            if (x < this.Parent.Size.Width)
            {
                this.lbTip.Location = new Point(e.Location.X + this.Parent.Location.X + this.Location.X + 10, e.Location.Y + this.Parent.Location.Y + this.Location.Y + 10);
            }
            else
            {
                this.lbTip.Location = new Point(e.Location.X + this.Parent.Location.X + this.Location.X - lbTip.Width - 10, e.Location.Y + this.Parent.Location.Y + this.Location.Y + 10);
            }
            this.lbTip.BringToFront();
            this.lbTip.Visible = true;
        }
    }
}
